package com.abel.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.abel.domain.OptionalInformation;
import com.abel.domain.PersonProfile;
import com.abel.domain.types.MaritalStatus;
import com.abel.manager.PersonProfileManager;
import com.abel.util.UserSessionLocator;
import com.abel.web.view.OptionalInfoViewBean;

public class OptionalInfoController extends SimpleFormController{

	protected final Log logger = LogFactory.getLog(getClass());
	
	private PersonProfileManager personProfileManager;
	
	private String previousView = null;
	
	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		logger.debug("@OptionalInfoController : onSubmit : entered");
		
		ModelAndView mav = null;
		
		Map<String, Object> modelMap = new HashMap<String, Object>();
		
		String action = request.getParameter("previous") != null ? "previous" : "continue";

		
		OptionalInfoViewBean bean = (OptionalInfoViewBean)command;
		OptionalInformation optionalInformation = new OptionalInformation();
		optionalInformation.setHeight(bean.getHeightInFeet() + (bean.getHeightInInches() != null && !bean.getHeightInInches().equals("") ? "." + bean.getHeightInInches() : ""));
		optionalInformation.setWeight(bean.getWeight());
		optionalInformation.setMaritalStatus(MaritalStatus.getMaritalStatusCode(bean.getMaritalStatus()));
		optionalInformation.setChildrensAges(bean.getChildrensAges());
		
		try {
			personProfileManager.updateOptionalInfo(optionalInformation);
			
		} catch (Exception e) {

			logger.error("@OptionalInfoController : onSubmit : exception " + e);
			errors.rejectValue("", "error.null",null,"A system error occurred while registering your information. Please log out and log back in.");
			return showForm(request,response,errors);

		}
		
		if (action != null && action.equals("previous")) {
			mav = new ModelAndView("redirect:" + getPreviousView());
		}
		else {
			mav = new ModelAndView("redirect:" + getSuccessView());
		}
		mav.addAllObjects(modelMap);
		return mav;
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		
		//get OptionalInformation from threadlocal
		PersonProfile personProfile = UserSessionLocator.getUserSession().getPerson().getApplication().getPersonProfile();
		OptionalInfoViewBean viewBean = new OptionalInfoViewBean();
		
		if (personProfile.getOptionalInformation() != null) {
			
			//information already exists - returning user
			OptionalInformation optionalInformation = personProfile.getOptionalInformation();
			viewBean = new OptionalInfoViewBean();
			
			String heightInFeet = null;
			String heightInInches = null;
			if (optionalInformation.getHeight()!= null 
									&& !optionalInformation.getHeight().equals("") 
									&& optionalInformation.getHeight().indexOf(".") != -1) {
				
				heightInFeet = optionalInformation.getHeight().split("[.]")[0];
				heightInInches = optionalInformation.getHeight().split("[.]")[1];
			}
			else {
				
				heightInFeet = optionalInformation.getHeight();
				heightInInches = "";
			}
			
			viewBean.setHeightInFeet(heightInFeet);
			viewBean.setHeightInInches(heightInInches);
			viewBean.setWeight(optionalInformation.getWeight());
			viewBean.setMaritalStatus(MaritalStatus.getMaritalStatusDesc(optionalInformation.getMaritalStatus()));
			viewBean.setChildrensAges(optionalInformation.getChildrensAges());
		}
		
		//setting marital status list
		viewBean.setMaritalStatusList(MaritalStatus.getMaritalStatusDescAsList());
		
		return viewBean;

	}

	/**
	 * @param personProfileManager the personProfileManager to set
	 */
	public void setPersonProfileManager(PersonProfileManager personProfileManager) {
		this.personProfileManager = personProfileManager;
	}

	/**
	 * @return the previousView
	 */
	public String getPreviousView() {
		return previousView;
	}

	/**
	 * @param previousView the previousView to set
	 */
	public void setPreviousView(String previousView) {
		this.previousView = previousView;
	}

	
}
